<?php

/**
 * Created by PhpStorm.
 * User: evan
 * Date: 16/8/24
 * Time: 上午11:06
 */
class Third_party_login extends MY_Public_Api_Controller
{
    public function login()
    {

        $uid    = trim($this->input->get_post('uid'));
        $uname  = trim($this->input->get_post('uname'));
        $header = trim($this->input->get_post('header'));
        $sex    = (int)$this->input->get_post('sex');
        $token  = trim($this->input->get_post('token'));
        $type   = (int)$this->input->get_post('type');
        $unionid= trim($this->input->get_post('unionid'));

        $this->load->service('user/service_user_third_party_login');

        $data = $this->service_user_third_party_login->login($uname, $token, $header, $sex, $type,$unionid);


        if($data['data']['uid'])
        {
            $tags = array();
            $data['data']['sex'] && $tags[]='sex_'.$data['data']['sex'];
            $data['data']['occupation'] && $tags[]='occupation_'.$data['data']['occupation'];

            $data['data']['tags'] = $tags;

            // token
            $this->load->service('user/service_user_auth');
            $token = $this->service_user_auth->generate_token($data['data']['uid']);
            $data['data']['token'] = $token['data']['token'];


            // 1内购  2公众号支持  3关闭
            $this->load->model('common/Model_conf', 'c');
            $conf_item = $this->c->getOne(array('name' => 'IOS_PAY'));

            $data['data']['open_pay'] = $conf_item['value'] ? $conf_item['value'] : 1;
        }


        $this->json_output($data['data'], $data['status']);
    }


    /**
     * 微信
     */
    public function weixin()
    {
        $uname = trim($this->input->get_post('uname'));
        $header= trim($this->input->get_post('header'));
        $sex   = (int)$this->input->get_post('sex');
        $token = trim($this->input->get_post('token'));
        $uid   = trim($this->input->get_post('uid'));


        $this->load->service('user/service_user_third_party_login');
        $data = $this->service_user_third_party_login->weixin($uname, $token, $header, $sex);
        
        if($data['data']['uid']){
        	
        	//用户标签
        	$this->load->model('user/Model_user_info', 'ui');
        	$ui_rs = $this->ui->getOne(array('uid' => $data['data']['uid']));
        	
        	$tags = array();
        	
        	if($ui_rs){
        		$ui_rs['occupation'] && $tags[]='occupation_'.$ui_rs['occupation'];
        		$ui_rs['sex'] && $tags[]='sex_'.$ui_rs['sex'];
        	}
        	
        	$data['data']['tags'] = $tags;
        	
        	// token
        	$this->load->service('user/service_user_auth');
        	$token = $this->service_user_auth->generate_token($data['data']['uid']);
        	$data['data']['token'] = $token['data']['token'];
        	
        	// 1内购  2公众号支持  3关闭
        	$this->load->model('common/Model_conf', 'c');
        	$conf_item = $this->c->getOne(array('name' => 'IOS_PAY'));
        	
        	$data['data']['open_pay'] = $conf_item['value'] ? $conf_item['value'] : 1;
        }

        $this->json_output($data['data'], $data['status']);
    }


    /**
     * 微博
     */
    public function weibo()
    {
        $uname = trim($this->input->get_post('uname'));
        $header= trim($this->input->get_post('header'));
        $sex   = (int)$this->input->get_post('sex');
        $token = trim($this->input->get_post('token'));
        $uid   = trim($this->input->get_post('uid'));


        $this->load->service('user/service_user_third_party_login');
        $data = $this->service_user_third_party_login->weibo($uname, $token, $header, $sex);
        
        if($data['data']['uid']){
        	 
        	//用户标签
        	$this->load->model('user/Model_user_info', 'ui');
        	$ui_rs = $this->ui->getOne(array('uid' => $data['data']['uid']));
        	 
        	$tags = array();
        	 
        	if($ui_rs){
        		$ui_rs['occupation'] && $tags[]='occupation_'.$ui_rs['occupation'];
        		$ui_rs['sex'] && $tags[]='sex_'.$ui_rs['sex'];
        	}
        	 
        	$data['data']['tags'] = $tags;
        	 
        	// token
        	$this->load->service('user/service_user_auth');
        	$token = $this->service_user_auth->generate_token($data['data']['uid']);
        	$data['data']['token'] = $token['data']['token'];
        	
        	// 1内购  2公众号支持  3关闭
        	$this->load->model('common/Model_conf', 'c');
        	$conf_item = $this->c->getOne(array('name' => 'IOS_PAY'));
        	 
        	$data['data']['open_pay'] = $conf_item['value'] ? $conf_item['value'] : 1;
        }


        $this->json_output($data['data'], $data['status']);
    }


    /**
     * QQ
     */
    public function qq()
    {
        $uname = trim($this->input->get_post('uname'));
        $header= trim($this->input->get_post('header'));
        $sex   = (int)$this->input->get_post('sex');
        $token = trim($this->input->get_post('token'));
        $uid   = trim($this->input->get_post('uid'));


        $this->load->service('user/service_user_third_party_login');
        $data = $this->service_user_third_party_login->qq($uname, $token, $header, $sex);
		
        if($data['data']['uid']){
        	 
        	//用户标签
        	$this->load->model('user/Model_user_info', 'ui');
        	$ui_rs = $this->ui->getOne(array('uid' => $data['data']['uid']));
        	 
        	$tags = array();
        	 
        	if($ui_rs){
        		$ui_rs['occupation'] && $tags[]='occupation_'.$ui_rs['occupation'];
        		$ui_rs['sex'] && $tags[]='sex_'.$ui_rs['sex'];
        	}
        	 
        	$data['data']['tags'] = $tags;
        	 
        	// token
        	$this->load->service('user/service_user_auth');
        	$token = $this->service_user_auth->generate_token($data['data']['uid']);
        	$data['data']['token'] = $token['data']['token'];
        	
        	// 1内购  2公众号支持  3关闭
        	$this->load->model('common/Model_conf', 'c');
        	$conf_item = $this->c->getOne(array('name' => 'IOS_PAY'));
        	 
        	$data['data']['open_pay'] = $conf_item['value'] ? $conf_item['value'] : 1;
        }

        $this->json_output($data['data'], $data['status']);
    }
}
/**
token: C04B23C247497510D86EEA62236340F0
gender: m
icon: http://q.qlogo.cn/qqapp/1105602488/916DF005EC533528510FB6C0A0FE0292/40
userId: 916DF005EC533528510FB6C0A0FE0292
userName: joyo
 */